////////////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2003-2006 Adobe Macromedia Software LLC and its licensors.
//  All Rights Reserved. The following is Source Code and is subject to all
//  restrictions on such code as contained in the End User License Agreement
//  accompanying this product.
//
////////////////////////////////////////////////////////////////////////////////

package mx.charts.renderers
{

import flash.display.Graphics;
import mx.charts.series.items.HLOCSeriesItem;
import mx.core.IDataRenderer;
import mx.graphics.IStroke;
import mx.graphics.Stroke;
import mx.skins.ProgrammaticSkin;

/**
 *  The default item Rnderer for an HLOCSeries object.
 *  This class renders a vertical line from high to low
 *  with tick marks at the open and close points of the chart item.
 *	<p>The vertical line is rendered by using the value of the owning series's
 *  <code>stroke</code> style. 
 *  The open and close tick marks are rendered by using the owning series's
 *  <code>openTickStroke</code> and <code>closeTickStroke</code> styles,
 *  respectively.
 *  The lengths of the open and close tick marks are determined by the
 *  owning series's <code>openTickLength</code> and
 *  <code>closeTickLength</code>styles, respectively.</p>
 *
 *	<p>Both the open tick and close tick values are optional;
 *  an HLOCItemRenderer only renders the tick marks if the open and close
 *  values on the associated chart item are non-<code>NaN</code> values.</p>
 */
public class HLOCItemRenderer extends ProgrammaticSkin implements IDataRenderer
{
    include "../../core/Version.as";

    //--------------------------------------------------------------------------
    //
    //  Constructor
    //
    //--------------------------------------------------------------------------

	/**
	 *  Constructor.
	 */
	public function HLOCItemRenderer() 
	{
		super();
	}

    //--------------------------------------------------------------------------
    //
    //  Properties
    //
    //--------------------------------------------------------------------------

    //----------------------------------
	//  data
    //----------------------------------

	/**
	 *  @private
	 *  Storage for the data property.
	 */
	private var _chartItem:HLOCSeriesItem;

	[Inspectable(environment="none")]

	/**
	 *  The chart item that this renderer represents.
	 *  HLOCItemRenderers assume this value
	 *  is an instance of HLOCSeriesItem.
	 *  This value is assigned by the owning series.
	 */
	public function get data():Object
	{
		return _chartItem;
	}

	/**
	 *  @private
	 */
	public function set data(value:Object):void
	{
		_chartItem = value as HLOCSeriesItem;

		invalidateDisplayList();
	}
	
    //--------------------------------------------------------------------------
    //
    //  Overridden methods
    //
    //--------------------------------------------------------------------------

	/**
	 *  @private
	 */
	override protected function updateDisplayList(unscaledWidth:Number,
												  unscaledHeight:Number):void
	{
		super.updateDisplayList(unscaledWidth, unscaledHeight);

		var stroke:IStroke = getStyle("stroke");
		
		var w2:Number = unscaledWidth / 2;

		var openTickLen:Number = Math.min(w2, getStyle("openTickLength"));
		var closeTickLen:Number = Math.min(w2, getStyle("closeTickLength"));

		var openTickStroke:Stroke = getStyle("openTickStroke");
		var closeTickStroke:Stroke = getStyle("openTickStroke");

		var openTick:Number;
		var closeTick:Number;
		
			
		if (_chartItem != null)
		{
			var lowValue:Number = Math.max(_chartItem.low,Math.max(_chartItem.high,_chartItem.close));
			var highValue:Number = Math.min(_chartItem.low,Math.min(_chartItem.high,_chartItem.close));
			if(!isNaN(_chartItem.open)) 
			{
				lowValue = Math.max(lowValue,_chartItem.open);
				highValue = Math.min(highValue,_chartItem.open);
			}
	
			var HLOCHeight:Number = lowValue - highValue;
			var heightScaleFactor:Number = (HLOCHeight > 0)? (height / HLOCHeight):0;

			openTick = (_chartItem.open - highValue) *
					   heightScaleFactor;
			closeTick = (_chartItem.close - highValue) *
						heightScaleFactor;
		}
		else
		{
			openTick = 0.75 * height;
			closeTick = 0.25 * height;
		}	
		
		var g:Graphics = graphics;
		g.clear();				
		stroke.apply(g);
		g.moveTo(w2, 0);
		g.lineTo(w2, height);
		if (!isNaN(openTick))
		{
			openTickStroke.apply(g);
			g.moveTo(w2, openTick);
			g.lineTo(w2 - openTickLen, openTick);
		}
		if (!isNaN(closeTick))
		{
			closeTickStroke.apply(g);
			g.moveTo(w2, closeTick);
			g.lineTo(w2 + closeTickLen, closeTick);	
		}
	}
}

}
